import { request } from "./index";
import type {
  LoginRequest,
  RegisterRequest,
  LoginResponse,
  RegisterResponse,
} from "@/types/user";

// 认证API接口
export const authApi = {
  /**
   * 用户登录
   */
  login(data: LoginRequest): Promise<LoginResponse> {
    return request.post("/auth/login", data, {
      showLoading: true,
      loadingText: "登录中...",
    });
  },

  /**
   * 用户注册
   */
  register(data: RegisterRequest): Promise<RegisterResponse> {
    return request.post("/auth/register", data, {
      showLoading: true,
      loadingText: "注册中...",
    });
  },

  /**
   * 用户登出
   */
  logout(): Promise<void> {
    return request.post(
      "/auth/logout",
      {},
      {
        showLoading: false,
      },
    );
  },

  /**
   * 刷新访问令牌
   */
  refreshToken(
    refreshToken: string,
  ): Promise<{ token: string; refreshToken?: string }> {
    return request.post(
      "/auth/refresh",
      { refreshToken },
      {
        showLoading: false,
      },
    );
  },

  /**
   * 发送验证码
   */
  sendVerificationCode(
    email: string,
    type: "register" | "reset" | "login" = "register",
  ): Promise<void> {
    return request.post(
      "/auth/send-code",
      { email, type },
      {
        showLoading: true,
        loadingText: "发送中...",
      },
    );
  },

  /**
   * 验证邮箱验证码
   */
  verifyCode(
    email: string,
    code: string,
    type: "register" | "reset" | "login" = "register",
  ): Promise<{ valid: boolean }> {
    return request.post("/auth/verify-code", { email, code, type });
  },

  /**
   * 重置密码
   */
  resetPassword(
    email: string,
    code: string,
    newPassword: string,
  ): Promise<void> {
    return request.post(
      "/auth/reset-password",
      {
        email,
        code,
        newPassword,
      },
      {
        showLoading: true,
        loadingText: "重置中...",
      },
    );
  },

  /**
   * 检查用户名是否可用
   */
  checkUsername(username: string): Promise<{ available: boolean }> {
    return request.get("/auth/check-username", { username });
  },

  /**
   * 检查邮箱是否可用
   */
  checkEmail(email: string): Promise<{ available: boolean }> {
    return request.get("/auth/check-email", { email });
  },

  /**
   * 获取当前用户信息
   */
  getCurrentUser(): Promise<LoginResponse["user"]> {
    return request.get("/auth/me");
  },

  /**
   * 验证令牌有效性
   */
  validateToken(): Promise<{ valid: boolean; user?: LoginResponse["user"] }> {
    return request.get("/auth/validate", {
      showLoading: false,
    });
  },

  /**
   * 社交登录 - Google
   */
  googleLogin(token: string): Promise<LoginResponse> {
    return request.post(
      "/auth/google",
      { token },
      {
        showLoading: true,
        loadingText: "Google登录中...",
      },
    );
  },

  /**
   * 社交登录 - GitHub
   */
  githubLogin(code: string): Promise<LoginResponse> {
    return request.post(
      "/auth/github",
      { code },
      {
        showLoading: true,
        loadingText: "GitHub登录中...",
      },
    );
  },

  /**
   * 绑定社交账号
   */
  bindSocialAccount(
    provider: "google" | "github",
    token: string,
  ): Promise<void> {
    return request.post(
      "/auth/bind-social",
      { provider, token },
      {
        showLoading: true,
        loadingText: "绑定中...",
      },
    );
  },

  /**
   * 解绑社交账号
   */
  unbindSocialAccount(provider: "google" | "github"): Promise<void> {
    return request.delete(
      "/auth/unbind-social",
      { provider },
      {
        showLoading: true,
        loadingText: "解绑中...",
      },
    );
  },

  /**
   * 启用两步验证
   */
  enableTwoFactor(): Promise<{
    qrCode: string;
    secret: string;
    backupCodes: string[];
  }> {
    return request.post(
      "/auth/2fa/enable",
      {},
      {
        showLoading: true,
        loadingText: "设置中...",
      },
    );
  },

  /**
   * 确认两步验证
   */
  confirmTwoFactor(code: string): Promise<{ backupCodes: string[] }> {
    return request.post(
      "/auth/2fa/confirm",
      { code },
      {
        showLoading: true,
        loadingText: "验证中...",
      },
    );
  },

  /**
   * 禁用两步验证
   */
  disableTwoFactor(password: string): Promise<void> {
    return request.post(
      "/auth/2fa/disable",
      { password },
      {
        showLoading: true,
        loadingText: "禁用中...",
      },
    );
  },

  /**
   * 验证两步验证码
   */
  verifyTwoFactor(code: string): Promise<{ valid: boolean }> {
    return request.post("/auth/2fa/verify", { code });
  },

  /**
   * 生成新的备份码
   */
  generateBackupCodes(): Promise<{ backupCodes: string[] }> {
    return request.post(
      "/auth/2fa/backup-codes",
      {},
      {
        showLoading: true,
        loadingText: "生成中...",
      },
    );
  },

  /**
   * 获取登录历史
   */
  getLoginHistory(
    page = 0,
    size = 20,
  ): Promise<{
    content: Array<{
      id: string;
      ip: string;
      userAgent: string;
      location?: string;
      loginTime: string;
      success: boolean;
    }>;
    totalElements: number;
    totalPages: number;
  }> {
    return request.get("/auth/login-history", { page, size });
  },

  /**
   * 获取活跃会话
   */
  getActiveSessions(): Promise<
    Array<{
      id: string;
      ip: string;
      userAgent: string;
      location?: string;
      lastActivity: string;
      current: boolean;
    }>
  > {
    return request.get("/auth/sessions");
  },

  /**
   * 终止会话
   */
  terminateSession(sessionId: string): Promise<void> {
    return request.delete(
      "/auth/sessions",
      { sessionId },
      {
        showLoading: true,
        loadingText: "终止中...",
      },
    );
  },

  /**
   * 终止所有其他会话
   */
  terminateAllOtherSessions(): Promise<void> {
    return request.delete(
      "/auth/sessions/others",
      {},
      {
        showLoading: true,
        loadingText: "终止中...",
      },
    );
  },
};
